﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _03.collection
{
    class Program
    {
        //集合分为两种：泛型集合、非泛型集合。泛型集合更好，因为非泛型集合通常是object类型，涉及到装箱、拆箱，效率较低。
        //非泛型：System.Collections下
        //泛型：System.Collections.Generic下
        static void Main(string[] args)
        {
            {//泛型List [public class List<T> : IList<T>, ICollection<T>, IEnumerable<T>, IEnumerable, IList, ICollection, IReadOnlyList<T>, IReadOnlyCollection<T>]
                List<int> list = new List<int>
                {
                    1,
                    2,
                    3
                };
                Console.WriteLine("list count: {0}", list.Count);
                list.ForEach(e => Console.WriteLine(e));
                string s = string.Join(",", list);
                Console.WriteLine(s);

                Console.WriteLine("list.ElementAt(1): {0}", list.ElementAt(1));//
                Console.WriteLine("list[1]: {0}", list[1]);//卧槽，这么用也可以啊
                bool b = list.Remove(9);
                //list.RemoveAt(9); //超出索引范围了！err
                int n = list.RemoveAll(i => i > 3);//删除所有符合条件的

                Console.WriteLine(string.Join(", ", list));
                list.Clear();
                Console.WriteLine(string.Join(", ", list));

                list.Add(1);
                list.Add(1);
                //索引访问 - 越界会异常! 支持读写！ public T this[int index]{get;set;}
                Console.WriteLine("list[0]: ={0}", list[0]);

                //也可以这样
                var lst = new List<int>() { 1, 2, 3 };

                for(int i = 0; i < lst.Count; i++)
                {
                    Console.WriteLine(lst[i]); //
                }
                foreach(var i in lst)
                {
                    Console.WriteLine(i);
                }

                Console.WriteLine("----------");
            }
            {//非泛型List [public class ArrayList : IList, ICollection, IEnumerable, ICloneable]
                ArrayList list = new ArrayList
                {
                    1,
                    2,
                    3
                };
                Console.WriteLine("list count: {0}", list.Count);

                Console.WriteLine(string.Join(",", list));
                Console.WriteLine(string.Join(",", list.ToArray()));

                //list.GetRange(1, 1);//卧槽，没有单独的索引？
                Console.WriteLine(list[1]);//卧槽，索引这么用啊
                Console.WriteLine("----------");
            }

            {//泛型Dictionary
                Dictionary<int, string> dic = new Dictionary<int, string>
                {
                    { 1, "1" },
                    { 2, "2" },
                    { 3, "3" },
                    { 9, "3" }
                };
                Console.WriteLine("dic.Count: {0}", dic.Count);
                //Dictionary<int, string>.Enumerator enum=dic.GetEnumerator();
                var e = dic.GetEnumerator();
                while (e.MoveNext())
                {
                    Console.WriteLine("{0}: {1}", e.Current.Key, e.Current.Value);
                }

                Console.WriteLine("dic.ContainsKey(1): {0}", dic.ContainsKey(1));
                Console.WriteLine("dic.ContainsKey(5): {0}", dic.ContainsKey(5));
                Console.WriteLine("dic.ContainsValue(\"1\"): {0}", dic.ContainsValue("1"));
                Console.WriteLine("dic.ContainsValue(\"5\"): {0}", dic.ContainsValue("5"));
                Console.WriteLine("dic[1]: {0}", dic[1]);
                //Console.WriteLine("dic[5]: {0}", dic[5]);//FIXME 关键字不存在的，报错
                Console.WriteLine("dic[9]: {0}", dic[9]);
                Console.WriteLine("----------");
            }

            {//非泛型Hashtable
                Hashtable dic = new Hashtable
                {
                    { 1, 1 },
                    { 2, 2 },
                    { 3, 3 }
                };
                Console.WriteLine("dic.ContainsKey(1): {0}", dic.ContainsKey(1));
                Console.WriteLine("dic.ContainsKey(5): {0}", dic.ContainsKey(5));
                Console.WriteLine("dic.ContainsValue(1): {0}", dic.ContainsValue(1));
                Console.WriteLine("dic.ContainsValue(\"1\"): {0}", dic.ContainsValue("1"));
                Console.WriteLine("dic[1]: {0}", dic[1]);
                Console.WriteLine("dic[5]: {0}", dic[5]);

                Console.WriteLine("----------");
            }

            {//泛型HashSet
                HashSet<int> set = new HashSet<int>
                {
                    1,
                    1,
                    2,
                    3
                };
                Console.WriteLine("set.Count: {0}", set.Count);
                Console.WriteLine(string.Join(", ", set));
                Console.WriteLine("set.Contains(1): {0}", set.Contains(1));
                Console.WriteLine("set.Contains(5): {0}", set.Contains(5));

                Console.WriteLine("----------");
            }

            Console.ReadKey();
        }
    }
}
